class Admin::DistrictsController < Admin::AdminApplicationController
 layout "admin"
 before_filter :admin_login_required
 
 def index
    @districts = District.paginate :page => params[:page]
 end
 
 def show
   @district = District.find(params[:id])
   @communities = Community.paginate :conditions => ["district_id = ?", @district.id], :order => "id ASC", :page => params[:page]
 end
 
 def import
    @district = District.find(params[:id])
    @datas = Array.new
    File.open("#{RAILS_ROOT}/public/data/#{@district.city.id}/#{@district.id}.txt")do|file|
      while line=file.gets
        @datas << line
      end
    end
    for data in @datas
      @district.communities.create(:title => data, :description => data, :position => data)
    end
    redirect_to admin_district_url(@district)
  end
  
  def create_forum
    @district = District.find(params[:id])
    for community in @district.communities
      if community.forum.nil?
        forum = Forum.new
	    forum.community_id = community.id
	    forum.save!
      end
    end
    redirect_to admin_district_url(@district)
  end
 
 def new
   @district = District.new(params[:district])
 end
 
 def create
 	@district = District.new(params[:district])
   @district.save
   redirect_to admin_district_url(@district)
 end
 
 def edit
   @district = District.find(params[:id])
 end
 
 def update
   @district = District.find(params[:id])
   @district.update_attributes(params[:district])
   redirect_to admin_district_url(@district)
 end
end
